package com.zxc.controller.permission;

import com.zxc.entity.SysPermission;
import com.zxc.entity.SysRole;
import com.zxc.service.SysPermissionService;
import com.zxc.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/permission")
public class PermissionController {

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/{pageNo}/{pageSize}")
    @ResponseBody
    public Map<String,Object> getAllPermission(@PathVariable Integer pageNo,
                                               @PathVariable Integer pageSize
                                               , HttpSession session){
        Map<String,Object> modelMap = new HashMap<>();
        //我们查询出来的角色权限，不包括系统管理员的角色，也就是当前登录用户所拥有的权限
        String sysUserRole = (String) session.getAttribute("sysUserRole");
        //根据当前角色名，查出对应的角色id
        SysRole sysRole = sysRoleService.getRoleIdByName(sysUserRole);
        Integer roleId = sysRole.getId();
        List<SysPermission> sysPermissionList = sysPermissionService.getSysPermissionList(pageNo,pageSize,roleId);
        modelMap.put("dataList",sysPermissionList);
        int total = sysPermissionService.getAllSysPermissionCount(roleId);
        modelMap.put("total",total);
        return modelMap;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> delete(String[] id){
        Map<String,Object> modelMap = new HashMap<>();
        int result = sysPermissionService.deleteByIds(id);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String,Object> save(SysPermission sysPermission){
        Map<String,Object> modelMap = new HashMap<>();
        if (sysPermission.getUrl() == null || sysPermission.getUrl().equals("")){
            sysPermission.setUrl(null);
        }
        int result = sysPermissionService.saveSysPermission(sysPermission);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("false",false);
        }
        return modelMap;
    }

    @RequestMapping(value = "/initPermission/{id}")
    @ResponseBody
    public Map<String,Object> initPermission(@PathVariable Integer id){
        Map<String,Object> modelMap = new HashMap<>();
        SysPermission sysPermission = sysPermissionService.getSysPermissionById(id);
        modelMap.put("sysPermission",sysPermission);
        return modelMap;
    }
}
